﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace SenseUtil
{
    class TabControlFile
    {
        public const string CSTR_TabControlFilename = "26948339_manila";
        public const string CSTR_WindowsPath = @"\Windows\";

        public static List<string[]> TabControlDetails()
        {
            string fullFilename = CSTR_WindowsPath + CSTR_TabControlFilename;
            List <string[]> installedTabs = new List<string[]>();

            //check file exists
            if (File.Exists(fullFilename))
            {
                try
                {
                    //Open as xml document for processing
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(fullFilename);
                    // Locate the root node and 
                    // Create a list of the child nodes of "Page"
                    XmlNodeList nodelist = xmldoc.DocumentElement.GetElementsByTagName("Page");//FirstChild;

                    //Populate the details for the tab
                    foreach (XmlNode aNode in nodelist)
                    {
                        XmlNode theNode;
                        string[] tabData = new string[3];
                        switch (aNode.Name)
                        {
                            case "Page":
                                //Get the attributes
                                theNode = aNode.Attributes.GetNamedItem("Name");
                                if (theNode != null)
                                {
                                    tabData[SenseUtil.xTabData.PageName]=theNode.Value;
                                }
                                break;
                        }

                        if (aNode.HasChildNodes)
                        {
                            for (int i = 0; i < aNode.ChildNodes.Count; i++)
                            {
                                XmlNode aChildNode = aNode.ChildNodes[i];
                                //Look through the compenent references
                                if (aChildNode.Name == "ComponentReference")
                                {
                                    //Find the component reference with Name = page to get the mode9 reference
                                    XmlNode subNodeName = aChildNode.Attributes.GetNamedItem("Name");
                                    XmlNode subNodeMode9 = aChildNode.Attributes.GetNamedItem("Mode9Path");
                                    if ((subNodeName != null) && (subNodeMode9 != null))
                                    {
                                        if (subNodeName.Value == "page")
                                        {
                                            tabData[SenseUtil.xTabData.Mode9Ref] = subNodeMode9.Value;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        installedTabs.Add(tabData);
                    }

                }
                catch
                {
                    MessageBox.Show("Unable to process the Tab Control file");
                }
            }

            return installedTabs;
        }
    
    }
}
